package com.javaAdmin.controller;

import com.javaAdmin.enums.ResultCodeEnum;
import com.javaAdmin.exection.BusinessException;
import com.javaAdmin.model.Student;
import com.javaAdmin.server.RestfulStudentServer;
import com.javaAdmin.util.Result;
import com.javaAdmin.validate.Save;
import com.javaAdmin.validate.Update;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/restful/student")
public class RestfulStudentController {

    @Resource
    private RestfulStudentServer restfulStudentServer;

    @PostMapping
    public Student save(@RequestBody @Validated(Save.class) Student student) {

        String name = student.getName();
        if ("51zxw".equals(name)) {
            throw new BusinessException("名字不能是51zxw");
        }

        restfulStudentServer.save(student);
        return student;
//        return Result.success(ResultCodeEnum.SUCCESS, student);
    }

    @PutMapping
    public Student update(@RequestBody @Validated(Update.class) Student student) {
        restfulStudentServer.updateById(student);
        return student;
    }

    @GetMapping
    public List<Student> list() {
        return restfulStudentServer.list();
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Integer id) {
        restfulStudentServer.deleteById(id);
    }

    @GetMapping("/{id}")
    public Student getInfoById(@PathVariable Integer id) {
        Student student = restfulStudentServer.getInfoById(id);
        System.out.println(student.toString());
        return student;
    }
}
